package org.jeecg.modules.gzgl.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zhaodui.fxy.*;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.gzgl.entity.FxyGzgl02;
import org.jeecg.modules.gzgl.service.IFxyGzgl02Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.service.impl.SysDataLogServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: 公证相关人员信息
 * @Author: zhaodui
 * @Date:   2020-02-06
 * @Version: V1.0
 */
@RestController
@RequestMapping("/gzgl/fxyGzgl02")
@Slf4j
public class FxyGzgl02Controller extends JeecgController<FxyGzgl02, IFxyGzgl02Service> {
	@Autowired
	private IFxyGzgl02Service fxyGzgl02Service;
	@Autowired
	private RedisUtil redisUtil;
	 @Autowired
	 private SysDataLogServiceImpl sysDataLogService;
	/**
	 * 分页列表查询
	 *
	 * @param fxyGzgl02
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FxyGzgl02 fxyGzgl02,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FxyGzgl02> queryWrapper = QueryGenerator.initQueryWrapper(fxyGzgl02, req.getParameterMap());
		Page<FxyGzgl02> page = new Page<FxyGzgl02>(pageNo, pageSize);
		IPage<FxyGzgl02> pageList = fxyGzgl02Service.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param fxyGzgl02
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FxyGzgl02 fxyGzgl02) {

		if(StringUtil.isEmpty(fxyGzgl02.getQuery39())||fxjConstant.iscommitfxy.equals(fxyGzgl02.getQuery39())){//不提交法信
			fxyGzgl02Service.save(fxyGzgl02);
			return Result.ok("添加成功！");
		}
		try{
			String appCode = fxyGzgl02.getQuery01();
			fxyUser fxyUser = new fxyUser();
			fxyUser.setMobile(fxyGzgl02.getQuery12());
			fxyUser.setName(fxyGzgl02.getQuery13());
			fxyUser.setIdType(fxyGzgl02.getQuery14());
			fxyUser.setIdCode(fxyGzgl02.getQuery15());
			fxyUser.setOrgId(fxyGzgl02.getQuery16());
			fxyUser.setEmail(fxyGzgl02.getQuery17());
			fxyUser.setAddress(fxyGzgl02.getQuery18());
			fxyUser.setThirdAccount(fxyGzgl02.getQuery19());
			String res = fxyUtil.registerperson(appCode,redisUtil,fxyUser);
			if(!StringUtil.isEmpty(res)){
				fxyGzgl02.setQuery20(res);
			}
		}catch (BaseBusinessException e){
			return Result.error(e.getMessage());
		}
		fxyGzgl02Service.save(fxyGzgl02);
		Gson gs = new GsonBuilder().serializeNulls().create();
		String datacont = gs.toJson(fxyGzgl02);
		String id = fxyGzgl02.getId();
		sysDataLogService.addDataLog("fxy_gzgl_02",id,  datacont);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param fxyGzgl02
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FxyGzgl02 fxyGzgl02) {

		if(StringUtil.isEmpty(fxyGzgl02.getQuery39())||fxjConstant.iscommitfxy.equals(fxyGzgl02.getQuery39())) {//不提交法信
			fxyGzgl02Service.updateById(fxyGzgl02);
			return Result.ok("编辑成功!");
		}
			try{
			String appCode = fxyGzgl02.getQuery01();
			fxyUser fxyUser = new fxyUser();
			fxyUser.setMobile(fxyGzgl02.getQuery12());
			fxyUser.setName(fxyGzgl02.getQuery13());
			fxyUser.setIdType(fxyGzgl02.getQuery14());
			fxyUser.setIdCode(fxyGzgl02.getQuery15());
			fxyUser.setOrgId(fxyGzgl02.getQuery16());
			fxyUser.setEmail(fxyGzgl02.getQuery17());
			fxyUser.setAddress(fxyGzgl02.getQuery18());
			fxyUser.setThirdAccount(fxyGzgl02.getQuery19());
			String res = fxyUtil.registerperson(appCode,redisUtil,fxyUser);
			if(!StringUtil.isEmpty(res)){
				fxyGzgl02.setQuery20(res);
			}
		}catch (BaseBusinessException e){
			return Result.error(e.getMessage());
		}
		fxyGzgl02Service.updateById(fxyGzgl02);

		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		fxyGzgl02Service.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.fxyGzgl02Service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		FxyGzgl02 fxyGzgl02 = fxyGzgl02Service.getById(id);
		if(fxyGzgl02==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(fxyGzgl02);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param fxyGzgl02
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FxyGzgl02 fxyGzgl02) {
        return super.exportXls(request, fxyGzgl02, FxyGzgl02.class, "公证相关人员信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FxyGzgl02.class);
    }

}
